//
//  BK_ReturnGoods_Progress_Query_VC.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/5/2.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_ReturnGoods_Progress_Query_VC.h"
#import "BK_ReturnGoods_Progress_Query_Cell.h"
#import "BKMyOrderReturnGoodsProgressModel.h"
#import "BKMyOrderRetuanGoodsProgressListController.h"
#import "BK_OD_Tool.h"
@interface BK_ReturnGoods_Progress_Query_VC ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property(nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSString* pape_no;//页数
@property(nonatomic,strong)NSString *total;


@property(nonatomic,strong) ExceptionView *exceptionView;



@end

@implementation BK_ReturnGoods_Progress_Query_VC



-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        
    }return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self  setProperty];
    [self  createUI];
    [self  check_NetWork];
    
}

-(void)setProperty{
    self.title = @"退换货进度查询";
    _pape_no = @"1";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor =RGB(234, 234, 234);
}

-(void)createUI{
    self.automaticallyAdjustsScrollViewInsets=NO;
    _tableView =[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionFooterHeight = 10;
    _tableView.backgroundColor =RGB(242, 242, 242);
    
    [_tableView registerClass:[BK_ReturnGoods_Progress_Query_Cell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
    }];
    [self setupRefresh];
    
}

-(void)check_NetWork{
    RequestReachabilityStatus status = [RequestManager reachabilityStatus];
    switch (status) {
        case RequestReachabilityStatusReachableViaWiFi:
        case RequestReachabilityStatusReachableViaWWAN: {
            
            [self.tableView.mj_header beginRefreshing];
        }
            break;
        default: {
            
            self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoNetwork];
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeNoNetwork handler:^(ExceptionViewAction *action) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
                [self check_NetWork];
            }];
            [self.exceptionView addAction:action];
            [self.exceptionView showInView:self.view];
        }
            break;
    }
    
    
}

-(void)getdata{
    
    [BKIndicationView showInView:self.tableView];
    
    [BKMyOrderGetDataFromNetWork GetOrderReturnGoodsProgress:@"20" pageno:self.pape_no Success:^(NSArray *Model) {
        
        [BKIndicationView dismiss];
        
        if ([self.pape_no isEqualToString:@"1"]) {
            [self.dataArr removeAllObjects];
        }
        [self.dataArr addObjectsFromArray:Model];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (self.dataArr.count ==0) {
            
            /*
             * 无数据
             */
            if (self.exceptionView) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
            }
            ExceptionCodeType errorType=ExceptionCodeTypeNoData;
            
            self.exceptionView = [ExceptionView exceptionViewWithType:errorType];
            self.exceptionView.contentLabel.text = @"";
            self.exceptionView.contentLabel.hidden=YES;
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:errorType handler:^(ExceptionViewAction *action) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
                [self getdata];
            }];
            
            
            [self.exceptionView addAction:action];
            [self.exceptionView showInView:self.view];
            
            
            
        }
        
    } failure:^(NSError *error) {
        [BKIndicationView dismiss];
        
        if (self.exceptionView) {
            [self.exceptionView dismiss];
            self.exceptionView = nil;
        }
        
        
        ExceptionCodeType errorType;
        if (error.code == -1009) {
            errorType = ExceptionCodeTypeNoNetwork;
        }else if (error.code == -1001){
            errorType = ExceptionCodeTypeRequestFailed;
        }else if (error.code == 502 || error.code == 404){
            errorType = ExceptionCodeTypeServerError;
        }else{
            errorType = ExceptionCodeTypeFailedToLoad;
        }
        
        self.exceptionView = [ExceptionView exceptionViewWithType:errorType];
        ExceptionViewAction *action = [ExceptionViewAction actionWithType:errorType handler:^(ExceptionViewAction *action) {
            [self.exceptionView dismiss];
            self.exceptionView = nil;
            [self getdata];
        }];
        
        [self.exceptionView addAction:action];
        [self.exceptionView showInView:self.view];
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } currentPage:^(NSDictionary *Page) {
        self.total = Page[@"total"];
        self.pape_no = Page[@"pageno"];
        if (self.dataArr.count>0 &&( self.dataArr.count < [self.total integerValue])) {
            self.tableView.mj_footer.hidden  = NO;
        }else{
            self.tableView.mj_footer.hidden  = YES;
        }
    }];
    
}

-(void)setupRefresh{
    BKRefreshGifHeader *headerRefresh = [BKRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.tableView.mj_header = headerRefresh;
    BKMyOrderFooterAutoReferesh *footer = [BKMyOrderFooterAutoReferesh footerWithRefreshingTarget:self refreshingAction:@selector(updataMore)];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
    
}

-(void)refreshData{
    self.pape_no = @"1";
    
    [self getdata];
    
}

-(void)updataMore{
    NSInteger page = [self.pape_no integerValue] +1;
    self.pape_no = [NSString stringWithFormat:@"%ld",(long)page];
    [self getdata];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    BKMyOrderReturnGoodsProgressModel *model = self.dataArr[section];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
    view.tag = section;
    view.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonAction:)];
    [view addGestureRecognizer:tapGesture];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *statsLabel = [[UILabel alloc]initWithFrame:CGRectMake(ApplicationBackGauge, 9.4, 40, 21)];
    statsLabel.textColor = [UIColor blackColor];
    statsLabel.text = @"状态:";
    statsLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:statsLabel];
    
    UILabel *statsNameL = [[UILabel alloc]initWithFrame:CGRectMake(ApplicationBackGauge+40, 9.5, 200, 21)];
    statsNameL.font = [UIFont systemFontOfSize:15];
    statsNameL.text = model.statusStr;
    if ([model.statusStr isEqualToString:@"退换货成功"]) {
        statsNameL.textColor = RGB(27, 188, 155);
        statsNameL.text = @"已完成";
    }
    else{
        statsNameL.textColor = [UIColor redColor];
    }
    [view addSubview:statsNameL];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // NSString *str =[NSString stringWithFormat:@"%@%@",model.typeStr,@"\U0000e618"];
    button.titleLabel.font =  [UIFont systemFontOfSize:14];
    [button setTitle:model.typeStr forState:UIControlStateNormal];
    [button setTitleColor:RGB(53, 53, 53) forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"bkmyorder_btn_my_nor"] forState:UIControlStateNormal];
    [button.titleLabel sizeToFit];
    [button.imageView sizeToFit];
    CGFloat labelWidth = button.titleLabel.frame.size.width;
    button.titleEdgeInsets = UIEdgeInsetsMake(0,-labelWidth, 0, labelWidth);
    button.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+25, 0, -labelWidth-25);
    button.tag = section;
    [button addTarget:self action:@selector(gotoList:) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.mas_right).offset(-ApplicationBackGauge);
        make.centerY.equalTo(view.mas_centerY);
        make.height.mas_equalTo(14);
        
        
    }];
    return view;
}

-(void)buttonAction:(UITapGestureRecognizer *)button{
    NSInteger tagValue = button.view.tag;
    BKMyOrderRetuanGoodsProgressListController *scheduleVC = [[ BKMyOrderRetuanGoodsProgressListController alloc]init];
    scheduleVC.model = self.dataArr[tagValue];
    [self.navigationController pushViewController:scheduleVC animated:YES];
}
-(void)gotoList:(UIButton*)btn{
    
    NSInteger tagValue = btn.tag;
    BKMyOrderRetuanGoodsProgressListController *scheduleVC = [[ BKMyOrderRetuanGoodsProgressListController alloc]init];
    scheduleVC.model = self.dataArr[tagValue];
    [self.navigationController pushViewController:scheduleVC animated:YES];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BK_ReturnGoods_Progress_Query_Cell *backCell = [_tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    backCell.selectionStyle = UITableViewCellSelectionStyleNone;
    backCell.model = self.dataArr[indexPath.section];
    return backCell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
